/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Date;
/**
 *
 * @author Pisarchik_S
 */
public class SaveToFile {

    public static void DoSave(CModel model)
    {

        Date d = new Date();
        String name = d.toString()+ ".txt";
        name = name.replace(':', '_');

        String card = "fileconn.dir.memorycard";
        String path = System.getProperty(card);
        System.out.println(path);
        try {
            String url = path + name;
            String string = model.GetString();
            byte data[] = Encoder.getBytes(string);
            FileConnection fconn = (FileConnection)Connector.open(url, Connector.READ_WRITE);
            if (!fconn.exists()) {
                fconn.create();
            }

            OutputStream ops = fconn.openOutputStream();
            ops.write(data);
            ops.close();
            fconn.close();
        }
        catch (IOException ioe) {
            System.out.println("IOException: "+ioe.getMessage());
        }
        catch (SecurityException se) {
            System.out.println("Security exception:" + se.getMessage());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
